"""
This file is part of GSBEdit. For copyright and licensing information, please
see gsbedit.py in the GSBEdit base directory.
"""


import wx
import os.path

import ui_elements.module_wizard as mw
import ui_elements.module_manager as mm
import game_data.detect_installs as di
import mod_selector as ms
import mod_editor as mod_e


MNU_EXIT = 1
MNU_CREATE_MODULE = 2
MNU_ABOUT = 3
BTN_CHANGE_MOD = 4


class MainWindow(wx.Frame):
    def __init__(self, gsbi):
        wx.Frame.__init__(self, None, -1, "GSB Modding Tool", size = (850, 600))
        
        self.gsbi = gsbi
        
        # Create a menu that allows us to open new windows
        file_menu = wx.Menu()
        file_menu.Append(MNU_EXIT, 'E&xit')

        create_menu = wx.Menu()
        create_menu.Append(MNU_CREATE_MODULE, '&Module')      
        
        help_menu = wx.Menu()
        help_menu.Append(MNU_ABOUT, '&About')

        # Create a menu bar and add the menu
        menu_bar = wx.MenuBar()
        menu_bar.Append (file_menu, '&File')
        #menu_bar.Append(create_menu, '&Create')
        menu_bar.Append(help_menu, '&Help')

        self.SetMenuBar(menu_bar)
        
        toolbar_p = wx.Panel(self)
        self.toolbar = wx.BoxSizer(wx.HORIZONTAL)
        self.current_mod_prelabel = wx.StaticText(toolbar_p, -1, 'Currently active mod: ')
        self.current_mod_label = wx.StaticText(toolbar_p, -1, 'Core data')
        change_mod = wx.Button(toolbar_p, BTN_CHANGE_MOD, 'Change mod...')
        self.toolbar.Add(self.current_mod_prelabel, 0, wx.LEFT | wx.TOP | wx.BOTTOM | wx.ALIGN_CENTER_VERTICAL, 10)
        self.toolbar.Add(self.current_mod_label, 0, wx.RIGHT | wx.TOP | wx.BOTTOM | wx.ALIGN_CENTER_VERTICAL, 10)
        self.toolbar.Add((10, 1), 0)
        self.toolbar.Add(change_mod, 0, wx.RIGHT | wx.TOP | wx.BOTTOM, 10)
        toolbar_p.SetSizer(self.toolbar)
        
        self.Bind(wx.EVT_MENU, self.exit, id=MNU_EXIT)
        #self.Bind(wx.EVT_MENU, self.start_module_wizard, id=MNU_CREATE_MODULE)
        self.Bind(wx.EVT_MENU, self.show_about, id=MNU_ABOUT)
        
        self.Bind(wx.EVT_BUTTON, self.change_mod, id=BTN_CHANGE_MOD)
        
        self.Bind(wx.EVT_CLOSE, self.close)
        
        #m = ms.ModSelectorWindow(self, self.set_new_mod, True)

        self.main_sizer = wx.BoxSizer(wx.VERTICAL)
        
        self.tabs = wx.Notebook(self)
        self.module_manager = mm.ModuleManager(self.tabs, self, self.gsbi)
        self.mod_editor = mod_e.ModEditor(self.tabs)
        self.tabs.AddPage(self.mod_editor, 'Mod')
        self.tabs.AddPage(wx.Panel(self.tabs, -1), 'Races')
        self.tabs.AddPage(self.module_manager, 'Modules')
        
        self.main_sizer.Add(toolbar_p, 0, wx.EXPAND)
        self.main_sizer.Add(wx.Panel(self, -1, size=(1,10)), 0, wx.EXPAND)
        self.main_sizer.Add(self.tabs, 1, wx.EXPAND)
        
        self.SetSizer(self.main_sizer)

        self.Center()
        
        self.set_new_mod(gsbi)
        #self.Show(True)
        
        icon = wx.Icon('img/gsbedit.ico', wx.BITMAP_TYPE_ICO, 16, 16)
        self.SetIcon(icon)


    def show_about(self, event):
        info = wx.AboutDialogInfo()
        info.SetIcon(wx.Icon('img/gsbedit.ico', wx.BITMAP_TYPE_ICO))
        info.SetName('GSBEdit')
        info.SetVersion('alpha')
        info.SetCopyright('(c) 2010-2013 John Kemp')
        info.SetWebSite('http://gsbedit.iualdii.net')
        info.SetLicence("See COPYING for license information.")
        wx.AboutBox(info)


    def exit(self, event):
        self.Close()
    
    def close(self, event):
        self.module_manager.closing()
        self.Destroy()
        

    def start_module_wizard(self, event):
        mw.ModuleWizard(self)
    
    
    def change_mod(self, event):
        m = ms.ModSelectorWindow(self, self.set_new_mod, True, has_exit=False)
    
    
    def get_current_mod(self):
        return self.gsbi.get_current_mod()
    
    
    def set_current_mod(self, mod):
        modname = mod.get_friendly_name()
        modtype = mod.get_type()
        if modtype != '':
            modtype = ' (%s)' % modtype
        self.current_mod_label.SetLabel('%s%s' % (modname, modtype))
    
    
    def set_new_mod(self, gsbi):
        self.gsbi = gsbi
        self.set_current_mod(gsbi.get_current_mod())
        self.module_manager.new_gsbi(gsbi)
        self.mod_editor.set_mod(gsbi.get_current_mod())
        self.toolbar.Layout()
        self.Show()
        self.Raise()
        
